<template>
  <div :class="className" >
    <el-row style="margin-bottom: 10px;">
      <el-col>
        <span style="font-weight: bold;margin-right: 5px;">{{charTitle}}</span>
        <el-tooltip class="item" effect="dark" content="刷新" placement="right" v-if="titleTips.length>0">
          <div slot="content">
            <div v-for="tip in titleTips">{{tip}}</div>
          </div>
          <el-button size="mini" circle icon="el-icon-thumb" />
        </el-tooltip>
      </el-col>
    </el-row>
    <div ref="echart" :style="{height:height,width:width}" />
  </div>
</template>

<script>
import * as echarts from 'echarts';
//require('echarts/theme/carp') // echarts theme
import resize from './mixins/resize'
import {parseTime} from "@/utils/ruoyi";

export default {
  mixins: [resize],
  props: {
    charTitle:{
      type: String,
      default: ''
    },
    titleTips:{
      type: Array,
      default: () => [],
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '250px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.echart)
      this.setOptions(this.chartData)
    },
    setOptions({ xAxisData, seriesData } = {}) {
      this.chart.setOption({
        grid:{
          show:false,
          top:'40',
          left:'5%',
          right:'2%',
          bottom:'30',
        },
        tooltip: {
          trigger: 'axis',
          confine: true,
          formatter: function(params) {
            var result = parseTime(params[0].name,'{y}年{m}月{d}日 {h}:{i}:{s}');
            params.forEach(function(item) {
              result += '<br/><span style="display:inline-block;width:10px;height:10px;border-radius:50%;background-color:' + item.color + ';margin-right:5px;"></span>' + item.seriesName + ': ' + item.value;
            });
            return result;
          }
        },
        legend: {
          type:'scroll',
          icon:'rect'
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        //color:[],
        xAxis: {
          type: 'category',
          data:xAxisData,
          axisLabel:{  //决定是否显示数据
            show:true,
            formatter: function(value,index){
              return parseTime(value,"{i}:{s}");
            }
          }
        },
        yAxis: {
          type: 'value'
        },
        series: seriesData,
      })
    }
  }
}
</script>
